package com.etc.service.impl;

import com.etc.dao.AuthorityMapper;
import com.etc.dao.UerInfomstMapper;
import com.etc.pojo.Authority;
import com.etc.pojo.CustomPo;
import com.etc.service.QuanXianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service
public class QuanXianServiceImpl implements QuanXianService {


    @Autowired
    UerInfomstMapper mapper;


    @Autowired
    AuthorityMapper amapper;

    @Override
    public List<CustomPo> findAllQuanxian(String uname) {

        return mapper.findByNameCutomer(uname);
    }

    @Override
    public List<Authority> findAllQuanxian1() {

        return amapper.findAll();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateQuanxian(HashMap param) {

        // 删除所有当前用户的权限

        amapper.deleteByUname(param.get("username").toString());

        // 插入新的权限
        for (Object qylist : ((List) param.get("qylist"))) {
            HashMap map = new HashMap();
            map.put("uname", param.get("username").toString());
            map.put("name", qylist.toString());
            amapper.insertAr(map);
        }

    }
}
